'use strict';

angular.module('afasApp')
  .factory('Services', ['$rootScope', '$http', '$q', 'ServicesLS', function ($rootScope, $http, $q, ServicesLS) {
    // Service logic
    // ...
    var property = [];

    // var api_host = "http://localhost:60896/api/";
    var api_host = "http://203.84.138.181/afasapi/api/";
    // var api_host = "http://192.168.1.21/IALF_AFAS/api/";
    // var api_host = "http://localhost/IALF_AFAS/api/";
    // var api_host = "";
    var key = ServicesLS.get('_auth');
    console.log(key);
    console.log(">>>>>>>>>>>>>>>>>>>>>>>" + key)
    if (key !== null) {
      // key = key.substr(0, key.length-1);
      // key = key.substr(1);
    };
    var config = {
        'Accept': 'application/json',
        // 'Content-Type': 'application/json',
        // 'Authorization': 'Basic QUZBUzohQUZBUw=='
        'Authorization': 'Basic '+key
        // 'Authorization': 'Basic d2lyeWFkaUB1cnJlbW90ZS5jb206IUFGQVM='
        // 'Access-Control-Allow-Headers': 'Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With'
    };
    // var api_host = "http://localhost:60896/api/";

    function handleError( response ) {
      // console.log(response);
      $rootScope.responseError.isError = false;
      if (response.status === 0) {
        $rootScope.responseError.message = 'Server is Down';
        $rootScope.responseError.isError = true;
        // console.log($rootScope.responseError);
      }
      if (response.status === 401) {
        $rootScope.responseError.message = 'You are not allowed to access this feature';
        $rootScope.responseError.isError = true;
        // ServicesLS.remove('_auth');
        // $state.go('login');
        // console.log($rootScope.responseError);
      }
      if (! angular.isObject( response.data ) || ! response.data.message ) {
        var results = [];
        results.data = response.data;
        if (response.headers) {
          results.headers = response.headers();
        };
        results.status = response.status;
        results.config = response.config;


        // return( results );
        return( $q.reject( results ) );

      }
      return( $q.reject( response.data.message ) );
    }
    function handleSuccess( response ) {
      var results = [];
      $rootScope.responseError.isError = false;
      results.data = response.data;
      results.headers = response.headers();
      results.status = response.status;
      results.config = response.config;

      return( results );
    }

    // Public API here
    return {
      getItems: function (entity, params) {
        params = typeof params !== 'undefined' ? params : {};
        $rootScope.myPromise = $http({
          url: api_host + entity + '/',
          method: 'GET',
          params: params,
          headers: config
        });
        return( $rootScope.myPromise.then( handleSuccess, handleError ) );
      },
      getItem: function(entity, id, params){
        params = typeof params !== 'undefined' ? params : {};
        $rootScope.myPromise = $http({
          url: api_host + entity + '/' + id,
          method: 'GET',
          params: params,
          headers: config
        });
        return( $rootScope.myPromise.then( handleSuccess, handleError ) );
      },
      add: function(entity, data){
        // data.LastUpdatedBy = 1;
        $rootScope.myPromise = $http({
          url: api_host + entity + '/',
          method: 'POST',
          data: data,
          headers: config
        });
        return( $rootScope.myPromise.then( handleSuccess, handleError ) );
      },
      update: function(entity, data, id, params){
        params = typeof params !== 'undefined' ? params : {};
        $rootScope.myPromise = $http({
          url: api_host + entity + '/' + id,
          method: 'PUT',
          params: params,
          data: data,
          headers: config
        });
        return( $rootScope.myPromise.then( handleSuccess, handleError ) );
      },
      delete: function(entity, id, params){
        params = typeof params !== 'undefined' ? params : {};
        $rootScope.myPromise = $http({
          url: api_host + entity + '/' + id,
          method: 'DELETE',
          params: params,
          headers: config
        });
        return( $rootScope.myPromise.then( handleSuccess, handleError ) );
      },
      getSelectedInvoice: function () {
        return property;
      },
      setSelectedInvoice: function(value) {
        property = value;
        console.log(value);
      }
    };
  }]);
